package es.ats.codington.web.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAttraction;
import es.ats.codington.festival.domain.TraditionalMarket;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.services.EventService;

/**
 * Servlet implementation class ShowEventsByPlaceServlet
 */
@WebServlet("/ShowEventsByPlaceServlet")
public class ShowEventsByPlaceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowEventsByPlaceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html");
		
		EventService es=new EventService();
		HttpSession session=request.getSession(true);
		ArrayList<Event> eventList = null;
		
		try {
			Event event=new Event();
			event.setPlace(request.getParameter("place"));
			session.setAttribute("place", event.getPlace());
			
			eventList = es.getShowEventsByPlace(event);
		 	session.setAttribute("eventByPlace",eventList);
		 	
		 	int id=0;
		 	if(event.getPlace().equals("Stadium")){
		 		id=1;
		 	}
		 	else if(event.getPlace().equals("Theater")){
		 		id=2;
		 	}
		 	else if(event.getPlace().equals("Museum")){
		 		id=3;
		 	}
		 	else if(event.getPlace().equals("Large Business")){
		 		id=4;
		 	}
		 	else if(event.getPlace().equals("Zoo")){
		 		id=5;
		 	}
		 	else if(event.getPlace().equals("Park")){
		 		id=6;
		 	}
		 	else if(event.getPlace().equals("Traditional Market")){
		 		id=7;
		 	}
		 	else if(event.getPlace().equals("Tourist Attraction")){
		 		id=8;
		 	}
		 	
		 	
		 	switch(id){
		 	
		 	case 1:
		 		Stadium stadium =new Stadium();
		 		stadium=es.showStadium(stadium);
		 		
		 		session.setAttribute("place_placeName",stadium.getPlaceName());
		 		session.setAttribute("place_zone",stadium.getZone());
		 		session.setAttribute("place_capacity",stadium.getCapacity());
		 		session.setAttribute("place_name",stadium.getStadiumName());
		 		break;
		 	case 2:
		 		Theater theater =new Theater();
		 		theater=es.showTheater(theater);
		 		
		 		session.setAttribute("place_placeName",theater.getPlaceName());
		 		session.setAttribute("place_zone",theater.getZone());
		 		session.setAttribute("place_capacity",theater.getCapacity());
		 		session.setAttribute("place_name",theater.getTheaterName());
		 		break;
		 	case 3:
		 		Museum museum =new Museum();
		 		museum=es.showMuseum(museum);
		 		
		 		session.setAttribute("place_placeName",museum.getPlaceName());
		 		session.setAttribute("place_zone",museum.getZone());
		 		session.setAttribute("place_capacity",museum.getCapacity());
		 		session.setAttribute("place_name",museum.getMuseumName());
		 		break;
		 	case 4:
		 		LargeBusiness largebusiness =new LargeBusiness();
		 		largebusiness=es.showLargeBusiness(largebusiness);
		 		
		 		session.setAttribute("place_placeName",largebusiness.getPlaceName());
		 		session.setAttribute("place_zone",largebusiness.getZone());
		 		session.setAttribute("place_capacity",largebusiness.getCapacity());
		 		session.setAttribute("place_name",largebusiness.getLargeBusinessName());
		 		break;
		 	case 5:
		 		Zoo zoo =new Zoo();
		 		zoo=es.showZoo(zoo);
		 		
		 		session.setAttribute("place_placeName",zoo.getPlaceName());
		 		session.setAttribute("place_zone",zoo.getZone());
		 		session.setAttribute("place_capacity",0);
		 		session.setAttribute("place_name",zoo.getZooName());
		 		break;
		 	case 6:
		 		Park park =new Park();
		 		park=es.showPark(park);
		 		
		 		session.setAttribute("place_placeName",park.getPlaceName());
		 		session.setAttribute("place_zone",park.getZone());
		 		session.setAttribute("place_capacity",0);
		 		session.setAttribute("place_name",park.getparkName());
		 		break;
		 	case 7:
		 		TraditionalMarket traditionalMarket =new TraditionalMarket();
		 		traditionalMarket=es.showTraditionalMarket(traditionalMarket);
		 		
		 		session.setAttribute("place_placeName",traditionalMarket.getPlaceName());
		 		session.setAttribute("place_zone",traditionalMarket.getZone());
		 		session.setAttribute("place_capacity",0);
		 		session.setAttribute("place_name",traditionalMarket.getMarketName());
		 		break;
		 	case 8:
		 		TouristAttraction touristAttraction =new TouristAttraction();
		 		touristAttraction=es.showTouristAttraction(touristAttraction);
		 		
		 		session.setAttribute("place_placeName",touristAttraction.getPlaceName());
		 		session.setAttribute("place_zone",touristAttraction.getZone());
		 		session.setAttribute("place_capacity",0);
		 		session.setAttribute("place_name",touristAttraction.getAttractionName());
		 		break;
		 	
		 	}
		 	
		 			 	
		 	RequestDispatcher rd=request.getRequestDispatcher("NewCodington_showEventsByPlace.jsp");  
    		rd.forward(request,response);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}
